#!/usr/bin/env python

import wx

class Contacts(object):
    '''Contacts class
    
    @author Julot <andy.julot@gmail.com>
    @since Thursday, February 23, 2012. 12:08 PM'''
    
    def __init__(self, view, presenter):
        self.view = view
        self.presenter = presenter
        self.bind()
    
    def bind(self):
        view  = self.view
        toolBar = view.contacts.toolBar
        view.Bind(wx.EVT_MENU, self.new, toolBar.new.tool)
        view.Bind(wx.EVT_MENU, self.save, toolBar.save.tool)
        view.Bind(wx.EVT_MENU, self.delete, toolBar.delete.tool)
        view.Bind(wx.EVT_LIST_ITEM_SELECTED, self.show, view.contacts.names)
        
        contacts = view.contacts
        view.Bind(wx.EVT_SEARCHCTRL_SEARCH_BTN, self.filter, contacts.filter)
        view.Bind(wx.EVT_SEARCHCTRL_CANCEL_BTN, self.clearFilter,
                  contacts.filter)
        view.Bind(wx.EVT_TEXT_ENTER, self.filter, contacts.filter)
        view.Bind(wx.EVT_LISTBOX, self.createContactMessage,
                  contacts.form.phones)
        view.Bind(wx.EVT_LISTBOX, self.createGroupMessage, contacts.form.groups)
    
    def new(self, event):
        self.presenter.contacts.new()
    
    def save(self, event):
        self.presenter.contacts.save()
    
    def show(self, event):
        self.presenter.contacts.show(event.Index, event.Data)
        self.presenter.message.create(event.Data)
    
    def createContactMessage(self, event):
        contact = self.view.contacts.form.model
        self.presenter.message.create(contact.id, [event.String])
    
    def createGroupMessage(self, event):
        self.presenter.message.create(groups=[event.String])
        
    def delete(self, event):
        self.presenter.contacts.delete()
    
    def filter(self, event):
        if event.String:
            self.presenter.contacts.filter(event.String)
    
    def clearFilter(self, event):
        if self.view.contacts.filter.Value:
            self.view.contacts.filter.Value = ''
            self.presenter.contacts.filter()
            